//
//  ThemeButtonModifier.swift
//  SwiftuiUno
//
//  Created by baozi on 2023/12/14.
//

import Foundation
import SwiftUI
// MARK: -- 全圆角背景
struct CapsuleBgModifier: ViewModifier {
    var colors: [Color]
    var shadow: Color = .clear
    var radius: CGFloat = 0
    private var bgColor: Color?
    init(colors: [Color], shadow: Color = .clear, radius: CGFloat = 0) {
        self.colors = colors
        self.shadow = shadow
        self.radius = radius
        if colors.count == 1 {
            bgColor = colors.first!
        }
    }
    func body(content: Content) -> some View {
        if let bgColor {
            content
                .background(
                    Capsule(style: .continuous)
                        .fill(
                            bgColor
                        )
                        .shadow(color: shadow, radius: radius)
                )
        } else {
            content
                .background(
                    Capsule(style: .continuous)
                        .fill(
                            LinearGradient(colors: colors, startPoint: .leading, endPoint: .trailing)
                        )
                        .shadow(color: shadow, radius: radius)
                )
        }
    }
}

// MARK: -- 全圆角边框
struct CapsuleBorderModifier: ViewModifier {
    var colors: [Color]
    private var singleColor: Color?
    init(colors: [Color]) {
        self.colors = colors
        if colors.count == 1 {
            singleColor = colors.first!
        }
    }
    
    func body(content: Content) -> some View {
        if let singleColor {
            content
                .overlay {
                    Capsule(style: .circular)
                        .stroke(lineWidth: 1)
                        .fill(
                            singleColor
                        )
                }
        } else {
            content
                .overlay {
                    Capsule(style: .circular)
                        .stroke(lineWidth: 1)
                        .fill(
                            LinearGradient(colors: colors, startPoint: .leading, endPoint: .trailing)
                        )
                }
        }
    }
}

// MARK: -- 过渡动画
struct RotationModifier: ViewModifier {
    var rotation: Double
    func body(content: Content) -> some View {
        content.rotationEffect(Angle(degrees: rotation))
            .offset(x: rotation != 0 ? UIScreen.main.bounds.width : 0,
                    y: rotation != 0 ? -UIScreen.main.bounds.height / 2 : 0)
    }
}

// MARK: -- 字体颜色大小字重
struct ThemeBtnTextStyleModifier: ViewModifier {
    var size: CGFloat
    var fontWeight: Font.Weight
    var color: Color
    func body(content: Content) -> some View {
        content
            .foregroundColor(color)
            .font(.system(size: size))
            .fontWeight(fontWeight)
    }
}

// MARK: -- 导航
struct NavStyleModifier: ViewModifier {
    @EnvironmentObject var route: Router
    var title: String
    func body(content: Content) -> some View {
        if route.path.count > 0 {
            content
                .navigationTitle(title)
                .navigationBarTitleDisplayMode(.inline)
                .navigationBarBackButtonHidden()
                .toolbar {
                    NavigationBackButton()
                }
        } else {
            content
                .navigationTitle(title)
                .navigationBarTitleDisplayMode(.inline)
                .navigationBarBackButtonHidden()
        }
    }
    
}

extension View {
    
    /// Text 风格
    /// - Parameters:
    ///   - font: 字体大小
    ///   - fontWeight: 字重
    ///   - color: 字体颜色
    func withTextStyle(font: CGFloat = 16, fontWeight: Font.Weight = .medium, color: Color = .white) -> some View {
        self.modifier(ThemeBtnTextStyleModifier(size: font, fontWeight: fontWeight, color: color))
    }
    
    /// 全圆角背景
    /// - Parameter colors: 背景颜色( 可单色 可渐变 )
    func withCapsuleBg(colors: [Color],
                       shadow: Color = .clear,
                       radius: CGFloat = 0) -> some View {
        self.modifier(CapsuleBgModifier(colors: colors, shadow: shadow, radius: radius))
    }
    
    /// 全圆角 主题背景颜色
    func withThemeRoundBg() -> some View {
        self.modifier(CapsuleBgModifier(colors: [Color.hex(0xFFDF66), Color.hex(0xFF12D7)]))
    }
    
    /// 全圆角边框背景
    /// - Parameter colors: color (可单色 可 渐变)
    func withCapsuleBorder(colors: [Color]) -> some View {
        self.modifier(CapsuleBorderModifier(colors: colors))
    }
}

// MARK: -- 导航
extension View {
    func setCustomNav(title: String) -> some View {
        modifier(NavStyleModifier(title: title))
    }
}

extension AnyTransition {
    static var rotating: AnyTransition {
        modifier(active: RotationModifier(rotation: 180), identity: RotationModifier(rotation: 0))
    }
    
}
